package com.trendplus.bmi;

import java.text.DecimalFormat;

import com.example.bmi.R;

import android.net.Uri;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



@TargetApi(9)
public class BMI extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmi);
        
        findViews();
        setListeners();
        setListenersCleanData();
    }
    
    private Button button_calc;
    private Button button_clean;
	private EditText field_height;
	private EditText field_weight;
	private TextView view_result;
	private TextView view_suggest;
	
	private void findViews(){
    	button_calc = (Button) findViewById(R.id.submit);
    	button_clean = (Button) findViewById(R.id.clearData);
        field_height = (EditText) findViewById(R.id.height);
        field_weight = (EditText) findViewById(R.id.weight);
        view_result = (TextView) findViewById(R.id.result);
        view_suggest = (TextView) findViewById(R.id.suggest);
    }
	
	//Listen for button clicks
    private void setListeners() {
        button_calc.setOnClickListener(calcBMI);
    }
    
    private void setListenersCleanData(){
    	button_clean.setOnClickListener(cleanEditText);
    }
    
    private void openOptionsDialog() {
    	new AlertDialog.Builder(BMI.this)
    	      .setTitle(R.string.about_title)
    	      .setMessage(R.string.about_msg)
    	      .setPositiveButton(R.string.ok_label,
    	            new DialogInterface.OnClickListener(){
    	                   public void onClick(
    	                        DialogInterface dialoginterface, int i){
    	                   }
    	            })
    	      .setNegativeButton(R.string.homepage_label,
    	            new DialogInterface.OnClickListener(){
    	                   public void onClick(DialogInterface dialoginterface, int i){
    	                       //go to url
    	                       Uri uri = Uri.parse(getString(R.string.homepage_label));
    	                       Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    	                       startActivity(intent);
    	                      }
    	              })
    	      .show();       
    }
    
    private void openErrOptionsDialog(){
    	new AlertDialog.Builder(BMI.this)
        .setMessage(R.string.error_msg_DataBlank)
        .setPositiveButton(R.string.ok_label,
            new DialogInterface.OnClickListener(){
                public void onClick(
                     DialogInterface dialoginterface, int i){
                }
           })
       
       .show();
    	
    	TextView result = (TextView)findViewById(R.id.result);
        TextView fieldsuggest = (TextView)findViewById(R.id.suggest);
        result.setText("");
        fieldsuggest.setText("");
    }

    private OnClickListener calcBMI = new OnClickListener(){
		public void onClick(View v) {
			 DecimalFormat nf = new DecimalFormat("0.00");
	         EditText fieldheight = (EditText)findViewById(R.id.height);
	         EditText fieldweight = (EditText)findViewById(R.id.weight);
	         
	         double BMI =0.0;
	         double height;
	         double weight;
	         String heightStr = fieldheight.getText().toString();
	         String weightStr = fieldweight.getText().toString();
	         
	         if(!heightStr.isEmpty() || !weightStr.isEmpty()){
	        	 height = Double.parseDouble(heightStr)/100;
			     weight = Double.parseDouble(weightStr);
			     BMI = weight / (height * height);
			     
			     TextView result = (TextView)findViewById(R.id.result);
		         result.setText("Your BMI is = "+nf.format(BMI));
		         
		         //view_result.setText(getText(R.string.bmi_result) + nf.format(BMI));
		         
		         //Give health advice
		         TextView fieldsuggest = (TextView)findViewById(R.id.suggest);
		         if(BMI>25){
		             fieldsuggest.setText(R.string.advice_heavy);
		         }else if(BMI<20){
		             fieldsuggest.setText(R.string.advice_light);
		         }else{
		             fieldsuggest.setText(R.string.advice_average);
		         }
	         }else{
	        	 openErrOptionsDialog();
	         }
	         //openOptionsDialog();
		}
    };
    
    private OnClickListener cleanEditText = new OnClickListener(){

		public void onClick(View v) {
			EditText fieldheight = (EditText)findViewById(R.id.height);
	        EditText fieldweight = (EditText)findViewById(R.id.weight);
	        TextView result = (TextView)findViewById(R.id.result);
	        TextView fieldsuggest = (TextView)findViewById(R.id.suggest);
	        
			fieldheight.setText("");
			fieldweight.setText("");
			result.setText("");
			fieldsuggest.setText("");
		}
    	
    };
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "about");
        menu.add(0, 1, 1, "leave");
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case 0:
            	openOptionsDialog();
                break;
            case 1:
                finish();
                break;
            default:
        }
        return super.onOptionsItemSelected(item);
    }
}
